package com.hualan.zhsq.service.impl.information;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hualan.zhsq.entity.information.HouseInfo;
import com.hualan.zhsq.mapper.information.HouseMapper;
import com.hualan.zhsq.service.information.HouseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service
public class HouseServiceImpl   implements HouseService {


    @Autowired
    private HouseMapper houseMapper;



    @Override
    public int add(HouseInfo houseInfo) {
        return houseMapper.insert(houseInfo);
    }

    @Override
    public int edit(HouseInfo houseInfo) {
        return houseMapper.updateById(houseInfo);
    }

    @Override
    public int remove(Integer id) {
        return houseMapper.deleteById(id);
    }

    @Override
    public int removeBatch(Integer[] ids) {
        return houseMapper.deleteByIds(Arrays.asList(ids));
    }

    @Override
    public List<HouseInfo> find() {
        Page<HouseInfo> houseInfoPage = houseMapper.select(null,null);

        return houseInfoPage.getRecords();
    }

    @Override
    public HouseInfo findById(Integer id) {
        return null;
    }

    /**
     * 根据房屋id和用户id查询
     * @param houseId
     * @param residentId
     * @return
     */
    @Override
    public HouseInfo findById(Integer houseId,Integer residentId) {

        return houseMapper.selectById(houseId,residentId);
    }
    /**
     * 分页查询
     * @param pageNum
     * @param pageSize
     * @param houseInfo
     * @return
     */
    @Override
    public Map<String, Object> findPage(Integer pageNum, Integer pageSize, HouseInfo houseInfo) {


        Page<HouseInfo> page =new Page<>(pageNum,pageSize);

        Page<HouseInfo> houseInfoPage = houseMapper.select(page, houseInfo);

        long pages = houseInfoPage.getPages();
        long total = houseInfoPage.getTotal();
        List<HouseInfo> houseInfos = houseInfoPage.getRecords();

        Map<String, Object> map = new HashMap<>();
        map.put("total",total);
        map.put("pages",pages);
        map.put("list",houseInfos);

        return map;
    }




}
